<template>
  <div>
    <div class="n-layout-page-header">
      <n-card :bordered="false" >
        <!--  这是由系统生成的CURD表格，你可以将此行注释改为表格的描述 -->
      </n-card>
    </div>
    <n-card :bordered="false" class="proCard" title="会员中心">
      
      <BasicTable  ref="actionRef"  :columns="columns" :request="loadDataTable" :row-key="(row) => row.id" :actionColumn="actionColumn" :scroll-x="scrollX" :resizeHeightOffset="-10000" :showTopRight="false" :paginate-single-page="false" >
        <template #tableTitle></template>
      </BasicTable>
    </n-card>
    <n-card :bordered="false" class="proCard" title="分销设置">
      <n-form :label-width="100" :model="formValue" ref="formRef">
        <n-grid :cols="24" :x-gap="24">
         
          <!-- <n-divider title-placement="left">微信支付</n-divider> -->
          
          <n-form-item-gi :span="6">
            <n-form-item label="销氪绑定商品-利润比例" path="distributionSalesKrBindProduct">
              <template #label>
                <n-icon size="18" style="margin-bottom: 4px;">
                  <GiftOutlined />
                </n-icon>
                销氪绑定商品-利润比例
              </template>
              <n-input-number v-model:value="formValue.distributionSalesKrBindProduct" :max="100" :precision="0">
                <template #suffix>
                  %
                </template>
              </n-input-number>
              <!-- <template #feedback>文本</template> -->
            </n-form-item>
          </n-form-item-gi>
          <n-form-item-gi :span="6">
            <n-form-item label="销氪绑定用户-利润比例" path="distributionSalesKrBindUser">
              <template #label>
                <n-icon size="18" style="margin-bottom: 4px;">
                  <UserOutlined />
                </n-icon>
                销氪绑定用户-利润比例
              </template>
              <n-input-number v-model:value="formValue.distributionSalesKrBindUser" :max="100" :precision="0">
                <template #suffix>
                  %
                </template>
              </n-input-number>
              <!-- <template #feedback>文本</template> -->
            </n-form-item>
          </n-form-item-gi>
          <n-form-item-gi :span="6">
            <n-form-item label="公司-利润比例" path="distributionCompanyProportion">
              <template #label>
                 <n-icon size="18" style="margin-bottom: 4px;">
                  <ApartmentOutlined />
                </n-icon>
                公司-利润比例
              </template>
              <n-input-number v-model:value="formValue.distributionCompanyProportion" :max="100" :precision="0">
                <template #suffix>
                  %
                </template>
              </n-input-number>
              <!-- <template #feedback>文本</template> -->
            </n-form-item>
          </n-form-item-gi>
          <n-form-item-gi :span="6">
            <n-form-item label="手续费" path="distributionSalesKrFee">
              <template #label>
                 <n-icon size="18" style="margin-bottom: 4px;">
                  <PayCircleOutlined />
                </n-icon>
                手续费
              </template>
              <n-input-number v-model:value="formValue.distributionSalesKrFee" :max="100" :precision="0">
                <template #suffix>
                  %
                </template>
              </n-input-number>
              <!-- <template #feedback>文本</template> -->
            </n-form-item>
          </n-form-item-gi>
        </n-grid>
         <div>
          <n-space>
            <n-button type="primary" @click="formSubmit">保存更新</n-button>
            <!--            <n-button type="default" @click="sendTest">测试支付</n-button>-->
          </n-space>
        </div>
      </n-form>
    </n-card>
    <Edit ref="editRef" @reloadTable="reloadTable" />
  </div>
</template>

<script lang="ts" setup>
  import { List, Status } from '@/api/memberCard';
import { getConfig, updateConfig } from '@/api/sys/config';
import { useForm } from '@/components/Form/index';
import { BasicTable, TableAction } from '@/components/Table';
import { usePermission } from '@/hooks/web/usePermission';
import { useDictStore } from '@/store/modules/dict';
import { adaTableScrollX } from '@/utils/hotgo';
import { ApartmentOutlined, GiftOutlined, PayCircleOutlined, UserOutlined } from '@vicons/antd';
import { useDialog, useMessage } from 'naive-ui';
import { computed, h, onMounted, reactive, ref } from 'vue';
import Edit from './edit.vue';
import { columns, loadOptions, State } from './model';

  const dict = useDictStore();
  const dialog = useDialog();
  const message = useMessage();
  const { hasPermission } = usePermission();
  const actionRef = ref();
  const searchFormRef = ref<any>({});
  const editRef = ref();

  const actionColumn = reactive({
    width: 144,
    title: '操作',
    key: 'action',
    align: 'center',
    fixed: 'right',
    render(record: State) {
      console.log(record.status === 1);
      
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
            auth: ['/memberCard/edit'],
          },

          {
            label: '下架',
            onClick: handleStatus.bind(null, record, 2),
            ifShow: () => {
              return record.status === 1;
            },
            type: 'default',
            auth: ['/memberCard/status'],
          },
          {
            label: '上架',
            onClick: handleStatus.bind(null, record, 1),
            ifShow: () => {
              return record.status === 2;
            },
            type: 'warning',
            auth: ['/memberCard/status'],
          },

        ],

      });
    },
  });

  const scrollX = computed(() => {
    return adaTableScrollX(columns, actionColumn.width);
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    // schemas,
  });

  const formValue = ref({
    distributionCompanyProportion: 0,
    distributionSalesKrBindProduct: 0,
    distributionSalesKrBindUser: 0,
    distributionSalesKrFee: 0,
  });

  const group = ref('distribution');
  const formRef: any = ref(null);
  function formSubmit() {
    if (formValue.value.distributionSalesKrBindUser + formValue.value.distributionSalesKrBindProduct + formValue.value.distributionCompanyProportion != 100) {
      message.error('请填写正确的比例');
      message.error(formValue.value.distributionSalesKrBindProduct + '% + ' + formValue.value.distributionSalesKrBindUser + '% + ' + formValue.value.distributionCompanyProportion + '% 不等于 100%');
      return;
    }
    formRef.value.validate((errors) => {
      if (!errors) {
        updateConfig({ group: group.value, list: formValue.value }).then((_res) => {
          message.success('更新成功');
          // load();
        });
      } else {
        message.error('验证失败，请填写完整信息');
      }
    });
  }

  function load() {
    loadOptions();
    getConfig({ group: group.value })
      .then((res) => {
        formValue.value = res.list;
      })
      .finally(() => {
      });
  }

  onMounted(() => {
    load();
  });

  // 加载表格数据
  const loadDataTable = async (res) => {
    return await List({ ...searchFormRef.value?.formModel, ...res });
  };

  // 重新加载表格数据
  function reloadTable() {
    actionRef.value?.reload();
  }

  // 编辑数据
  function handleEdit(record: Recordable) {
    editRef.value.openModal(record);
  }

  // 修改状态
  function handleStatus(record: Recordable, status: number) {
    Status({ id: record.id, status: status }).then((_res) => {
      message.success('设置成功');
      setTimeout(() => {
        reloadTable();
      });
    });
  }

  onMounted(() => {
    loadOptions();

  });
</script>

<style lang="less" scoped></style>